package cn.tedu.eb_film.utils;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.*;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;

/**
 * @author jie
 * 阿里云短信验证
 */
public class SmsUtils {

    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
       Config config = new Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId("LTAI5tCCLEgBZ55nsXQhuTvf")
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret("x1MNfy5nYFatzwe1nYr3BuULfqlWUb");
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new Client(config);
    }



    public static void sendSms(String phone, Integer code) throws Exception {
       Client client = SmsUtils.createClient("accessKeyId", "accessKeySecret");
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                //签名名称
                .setSignName("ebmovie")
                .setTemplateCode("SMS_276045259")
                .setPhoneNumbers(phone)
                .setTemplateParam("{\"code\":"+code+"}");
       RuntimeOptions runtime = new RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
        } catch (TeaException error) {
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }

    public static void main(String[] args) throws Exception {
        sendSms("15281613685",12345);
    }
}